package com.mew.slots.core.game.szxz.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.RandomIdUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.szxz.dto.SzxzInRoom;
import com.mew.slots.core.game.szxz.dto.SzxzPlayMsgs;
import com.mew.slots.core.game.szxz.roller.Controlline;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SzxzRoom extends BaseRoom {

    public static int _FREE =  11;
    public static int _SCATTER =  12;
    public static int _GOLDWILD = 15;//金狼狗

    private int[] courseTypes={0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,2};//0 普通免费 1 super 2 mega

    private int[] noLockGold={70,25,5};
    private int[] lockGold={85,15,0};

    private SzxzInRoom szxzInRoom;
    private int freeType;
    private int freeNum;
    public SzxzRoom(SzxzInRoom szxzInRoom, int freeTyp,int freeNum){
        this.szxzInRoom=szxzInRoom;
        this.freeType=freeTyp;
        this.freeNum=freeNum;
        res = new SzxzPlayMsgs();
        row = 3;
        col = 5;
        _WILD = 10;
        spes = new ArrayList<Integer>(){{
            add(11);
            add(12);
        }};

         rate = new int[][]{
              {0,  0,  5,  15,  50},//锅                  0
              {0,  0,  5,  15,  50},//炸药桶               1
              {0,  0,  5,  15,  50},//茅草房               2
              {0,  0,  5,  15,  50},//木房                 3
              {0,  0,  5,  15,  50},//砖瓦房               4
              {0,  0, 10,  25, 100},//狗                  5
              {0,  0, 10,  30, 150},//红猪                 6
              {0,  0, 10,  30, 150},//绿猪                 7
              {0,  0, 10,  30, 150},//蓝猪                 8
              {0,  5, 15,  50, 300},//猪妈妈               9
              {0,  5, 15,  50, 300},//_WILD               10
              {0,  0,  5,  10,  25},//FREE 金币猪          11
              {0,  0,  0,   0,   0},//_SCATTER 转盘        12
        };

        alllines = new int[][]{
                {5,6,7,8,9},
                {0,1,2,3,4},
                {10,11,12,13,14},
                {0,6,12,8,4},
                {10,6,2,8,14},
                {5,1,2,3,9},
                {5,11,12,13,9},
                {0,1,7,13,14},
                {10,11,7,3,4},
                {5,11,7,3,9},
                {5,1,7,13,9},
                {0,6,7,8,4},
                {10,6,7,8,14},
                {0,6,2,8,4},
                {10,6,12,8,14},
                {5,6,2,8,9},
                {5,6,12,8,9},
                {0,1,12,3,4},
                {10,11,2,13,14},
                {0,11,12,13,4},
                {10,1,2,3,14},
                {5,11,2,13,9},
                {5,1,12,3,9},
                {0,11,2,13,4},
                {10,1,12,3,14},
                {10,1,7,13,4},
                {0,11,7,3,14},
                {0,11,7,13,4},
                {10,1,7,3,14},
                {10,6,2,3,9},
                {0,6,12,13,9},
                {0,1,12,13,14},
                {10,11,2,3,4},
                {5,1,12,8,14},
                {5,11,2,8,4},
                {0,6,2,8,14},
                {10,6,12,8,4},
                {5,11,12,3,4},
                {0,1,7,8,14},
                {10,11,7,8,4},
                {10,1,2,3,4},
                {0,11,12,13,14},
                {10,11,12,13,4},
                {0,1,2,3,14},
                {5,1,7,3,9},
                {5,11,7,13,9},
                {0,6,12,13,14},
                {10,6,2,3,4},
                {0,6,7,8,9},
                {10,6,7,8,9}
        };
    }
    /**
     * 取线
     */
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts(free?1:0,rollerID);
        }

        replaceIcon();
    }
    private void replaceIcon(){
        if(free){
            replaceRemoveIcon();
            res.endSlots=endSlots.clone();
            //特殊免费
            if(freeType>0){
                computeFreeWild();
            }
        }else{
            res.endSlots=endSlots.clone();
        }
    }
    private void replaceRemoveIcon(){
        int temp=0;
        for (int i = 4; i >= 0; i--) {
            if(!szxzInRoom.getRemoveIcon().contains(i)){
                temp=i;
            }
        }
        for (int i = 0; i < endSlots.length; i++) {
            if(szxzInRoom.getRemoveIcon().contains(endSlots[i])){
                endSlots[i]=temp;
            }
        }
    }
    private void computeFreeWild(){
        SzxzPlayMsgs pmres=(SzxzPlayMsgs) res;
        int wildNum=computeGoldNumber();
        while (wildNum>0){
            int index=RandomUtil.randomInt(endSlots.length);
            if(!pmres.getWildIndex().contains(index)){
                endSlots[index]=_WILD;
                res.endSlots[index]=_GOLDWILD;
                pmres.getWildIndex().add(index);
                wildNum--;
            }
        }
        if(freeType==2){
            for(int lock:szxzInRoom.getLockWildIndex()){
                endSlots[lock]=_WILD;
            }
            for(int rw:pmres.getWildIndex()){
                if(!szxzInRoom.getLockWildIndex().contains(rw)){
                    szxzInRoom.getLockWildIndex().add(rw);
                }
            }
        }
    }
    private int computeGoldNumber(){
        int n=0;
        int[] ns={0,1,2};
        if(freeType==2){
            n=RandomIdUtil.produceRateRandomNumber(ns,lockGold);
        }else if(freeType==1){
            n=RandomIdUtil.produceRateRandomNumber(ns,noLockGold);
        }
        return n;
    }

    @Override
    public void freeBouns() {
        int[] courses = null;
        for (int[] cours : szxzInRoom.getCourses()) {
            if(cours[0]==(int)res.getBet()){
                courses=cours;
                break;
            }
        }
        if(courses==null){
            courses=new int[]{
                    (int)res.getBet(),0
            };
            szxzInRoom.getCourses().add(courses);
        }

        if(courses[1] == courseTypes.length - 1 && freeNum == 1){
            courses[1] = 0;
        }
        SzxzPlayMsgs pmres=(SzxzPlayMsgs) res;
        courses[1] = resolverFree(pmres,courses[1]);
        computeBonus();
        pmres.setCourse(courses[1]);
    }
    private int resolverFree(SzxzPlayMsgs pmres,int course){
        int freeNum=fsdb(_FREE);
        freeNum=freeNum>5?5:freeNum;
        if(freeNum>2){
            if(!free) {
                List<Integer> removeIcon = computeRemoveIcon();
                szxzInRoom.setRemoveIcon(removeIcon);
                szxzInRoom.getLockWildIndex().clear();
                course++;
                pmres.setFreeType(courseTypes[course]);
                pmres.setRemoveIcon(removeIcon);
            }

            pmres.setFree((byte)1);
            pmres.setFreeNumber(freeNum==3?7:freeNum==4?9:13);
            pmres.setMultiple(res.getMultiple()+rate[_FREE][freeNum-1]);
        }
        return course;
    }

    private List<Integer> computeRemoveIcon(){
        ArrayList<Integer> tempNum=new ArrayList<>();
        int dri=RandomUtil.randomInt(0,5);
        for (int i = 0; i < 5; i++) {
            if(dri!=i){
                tempNum.add(i);
            }
        }
        Collections.shuffle(tempNum);
        return tempNum;
    }
    private void computeBonus(){
        int snum=fsdb(_SCATTER);
        if(snum>2){
            res.setBouns(1);
        }
    }
    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return new int[]{
                        11,0,11,2,3,
                        4, 5, 6,7,8,
                        1,10,9,2,3
                };
/*                return bigwin();*/
            case 2:
                return controlline.free();
            case 3:
                return controlline.bonus();
            default:
                return controlline.empty();
        }
    }
}
